﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAL;

namespace BLL.Pages
{
    public class Partners:BasePages
    {
        #region "Constructors"
        public Partners() { }

        public Partners(int id, DateTime addedDate, string addedBy,string title,string logo, int importance, string website, string description,int partnerCatID, int option)
        {
            this.ID = id;
            this.AddedDate = addedDate;
            this.AddedBy = addedBy;
            this.Title = title;
            this.Logo = logo;
            this.Importance = importance;
            this.Website = website;
            this.Description = description;
            this.PartnerCatID = partnerCatID;
            this.Option = option;
        }
        #endregion

        #region "Public Properties"

        public string Title { get; set; }

        public string Logo { get; set; }

        public int Importance { get; set; }

        public string Website { get; set; }

        public string Description { get; set; }

        public int PartnerCatID { get; set; }

        public int Option { get; set; }

        #endregion

        #region Get Partner From PartnersDetail
        private static Partners GetPartnerFromPartnersDetail(PartnersDetails record)
        {
            if (record == null)
                return null;
            else
                return new Partners(record.PartnerID, record.AddedDate, record.AddedBy, record.Title,record.Logo, record.Importance, record.Website, record.Description,record.PartnerCatID,record.Option);
        }

        private static List<Partners> GetPartnerFromListPartnerDetail(List<PartnersDetails> recordset)
        {
            List<Partners> Partners = new List<Partners>();
            foreach (PartnersDetails record in recordset)
                Partners.Add(GetPartnerFromPartnersDetail(record));
            return Partners;
        }
        private static List<Partners> GetPartner5FromListPartnerDetail(List<PartnersDetails> recordset)
        {
            List<Partners> Partners = new List<Partners>();
            int flag = 0;
            foreach (PartnersDetails record in recordset)
                if (flag <= 5)
                {
                    flag = flag + 1;
                    Partners.Add(GetPartnerFromPartnersDetail(record));
                }
            return Partners;
        }
        #endregion

        #region Method
        public static Partners GetPartner(int PartnerID)
        {
            Partners Partner = null;
            string key = "Partner_" + PartnerID.ToString();
            if (BasePages.Settings.EnableCaching && BizObject.Cache[key] != null)
                Partner = (Partners)BizObject.Cache[key];
            else
            {
                Partner = GetPartnerFromPartnersDetail(SiteProvider.Pages.PartnerGet(PartnerID));
                BasePages.CacheData(key, Partner);
            }
            return Partner;
        }

        public static List<Partners> ListPartner()
        {
            List<Partners> Partners = null;
            string key = "Partners";
            if (BasePages.Settings.EnableCaching && BizObject.Cache[key] != null)
                Partners = (List<Partners>)BizObject.Cache[key];
            else
            {
                List<PartnersDetails> recordset = SiteProvider.Pages.ListPartner();
                Partners = GetPartnerFromListPartnerDetail(recordset);
                BasePages.CacheData(key, Partners);
            }
            return Partners;
        }

        public static List<Partners> ListBanner()
        {
            List<Partners> Partners = null;
            string key = "Partners";
            if (BasePages.Settings.EnableCaching && BizObject.Cache[key] != null)
                Partners = (List<Partners>)BizObject.Cache[key];
            else
            {
                List<PartnersDetails> recordset = SiteProvider.Pages.ListBanner();
                Partners = GetPartnerFromListPartnerDetail(recordset);
                BasePages.CacheData(key, Partners);
            }
            return Partners;
        }

        public static List<Partners> GetPartnerByPartnerCatID(int partnerCatID)
        {
            List<Partners> Partners = null;
            string key = "Partners_"+partnerCatID.ToString();
            if (BasePages.Settings.EnableCaching && BizObject.Cache[key] != null)
                Partners = (List<Partners>)BizObject.Cache[key];
            else
            {
                List<PartnersDetails> recordset = SiteProvider.Pages.GetPartnerByPartnerCatID(partnerCatID);
                Partners = GetPartnerFromListPartnerDetail(recordset);
                BasePages.CacheData(key, Partners);
            }
            return Partners;
        }

        public static List<Partners> GetBannerByPartnerCatID(int partnerCatID)
        {            
            List<Partners> Partners = null;
            if (partnerCatID == 2 || partnerCatID == 3)
            {
                string key = "Partners_" + partnerCatID.ToString();
                if (BasePages.Settings.EnableCaching && BizObject.Cache[key] != null)
                    Partners = (List<Partners>)BizObject.Cache[key];
                else
                {
                    List<PartnersDetails> recordset = SiteProvider.Pages.GetPartnerByPartnerCatID(partnerCatID);
                    Partners = GetPartnerFromListPartnerDetail(recordset);
                    BasePages.CacheData(key, Partners);
                }
                return Partners;
            }
            return Partners;
           
        }

        public static List<Partners> GetPartner5ByPartnerCatID(int partnerCatID)
        {
            List<Partners> Partners = null;
            string key = "Partners5_" + partnerCatID.ToString();
            if (BasePages.Settings.EnableCaching && BizObject.Cache[key] != null)
                Partners = (List<Partners>)BizObject.Cache[key];
            else
            {
                List<PartnersDetails> recordset = SiteProvider.Pages.GetPartnerByPartnerCatID(partnerCatID);
                Partners = GetPartner5FromListPartnerDetail(recordset);
                BasePages.CacheData(key, Partners);
            }
            return Partners;
        }

        public static int InsertPartner(string title,string logo, int importance, string website, string description,int partnerCatID, int option)
        {
            if (importance == null)
                importance = 0;
            if (partnerCatID == null)
                partnerCatID = 0;
            if (title == null)
                title = "";
            if (logo == null)
                logo = "";
            if (website == null)
                website = "";
            if (description == null)
                description = "";
            website = Helpers.ConvertNullToEmptyString(website);
            description = Helpers.ConvertNullToEmptyString(description);
            PartnersDetails record = new PartnersDetails(0, DateTime.Now,BizObject.CurrentUserName,title,logo,importance,website,description,partnerCatID,option);
            int ret = SiteProvider.Pages.InsertPartner(record);
            BizObject.PurgeCacheItems("Partner");
            return ret;
        }

        public static bool UpdatePartner(int id, string title,string logo, int importance, string website, string description,int partnerCatID)
        {
            if (importance == null)
                importance = 0;
            if (partnerCatID == null)
                partnerCatID = 0;
            if (title == null)
                title = "";
            if (logo == null)
                logo = "";
            if (website == null)
                website = "";
            if (description == null)
                description = "";
            website = Helpers.ConvertNullToEmptyString(website);
            description = Helpers.ConvertNullToEmptyString(description);
            PartnersDetails record = new PartnersDetails(id, DateTime.Now, BizObject.CurrentUserName, title,logo, importance, website, description,partnerCatID,0);
            bool ret = SiteProvider.Pages.UpdatePartner(record);
            BizObject.PurgeCacheItems("Partner");
            return ret;
        }

        public static bool UpdatePartnerOption(int id, string title, string logo, int importance, string website, string description, int partnerCatID, int option)
        {
            if (importance == null)
                importance = 0;
            if (partnerCatID == null)
                partnerCatID = 0;
            if (title == null)
                title = "";
            if (logo == null)
                logo = "";
            if (website == null)
                website = "";
            if (description == null)
                description = "";
            website = Helpers.ConvertNullToEmptyString(website);
            description = Helpers.ConvertNullToEmptyString(description);
            PartnersDetails record = new PartnersDetails(id, DateTime.Now, BizObject.CurrentUserName, title, logo, importance, website, description, partnerCatID,option);
            bool ret = SiteProvider.Pages.UpdatePartnerOption(record);
            BizObject.PurgeCacheItems("Partner");
            return ret;
        }

        public static bool DeletePartner(int id)
        {
            bool ret = SiteProvider.Pages.DeletePartner(id);
            BizObject.PurgeCacheItems("Partner");
            return ret;
        }
        #endregion
    }
}
